import random
import items,world
 
class Player():
    def __init__(self):
    	#gives the player their starting items
        self.inventory = [items.Gold(15), items.Rock()]
        #sets the player's health
        self.hp = 100
        #positions the player
        self.location_x, self.location_y = world.starting_position
        #stops the player from winning immediately
        self.victory = False
 	#checks if the player is alive
    def is_alive(self):
        return self.hp > 0
	#prints the player's inventory 
    def print_inventory(self):
        for item in self.inventory:
            print(item, '\n')
    #defines how actions are carried out
    def do_action(self, action, **kwargs):
        action_method = getattr(self, action.method.__name__)
        if action_method:
            action_method(**kwargs)
    #EXPERIMENTORY#########################################
    def take_item(self, tile):
    	if MapTile.inventory != []:
        	player.append(self.item)
    #EXPERIMENTORY#########################################
    #dictates what happens when the 'flee' action is called
    def flee(self, tile):
        #Moves the player randomly to an adjacent tile
        available_moves = tile.adjacent_moves()
        r = random.randint(0, len(available_moves) - 1)
        self.do_action(available_moves[r])
	#dictates what happens when north, south, east and west are called
    def move(self, dx, dy):
        self.location_x += dx
        self.location_y += dy
        print(world.tile_exists(self.location_x, self.location_y).intro_text())
    #dictates what happens when the 'north' action is called
    def move_north(self):
        self.move(dx=0, dy=-1)
    #dictates what happens when the 'south' action is called
    def move_south(self):
        self.move(dx=0, dy=1)
    #dictates what happens when the 'east' action is called
    def move_east(self):
        self.move(dx=1, dy=0)
    #dictates what happens when the 'west' action is called
    def move_west(self):
        self.move(dx=-1, dy=0)
	#dictates what happens when the 'attack' action is called
    def attack(self, enemy):
        best_weapon = None
        max_dmg = 0
        #sets the weapon and its max damage when the player attacks
        for i in self.inventory:
            if isinstance(i, items.Weapon):
                if i.damage > max_dmg:
                    max_dmg = i.damage
                    best_weapon = i
     	#prints what happens when the player attacks
        print("You use {} against {}!".format(best_weapon.name, enemy.name))
        enemy.hp -= best_weapon.damage
        if not enemy.is_alive():
            print("You killed {}!".format(enemy.name))
        else:
            print("{} HP is {}.".format(enemy.name, enemy.hp))